package com.zou.dataStruct.Graph.Linked;
/**
 5
 HUEAK
 0 0 2 3 0
 0 0 0 7 4
 2 0 0 0 0
 3 7 0 0 1
 0 4 0 1 0
 H
 */

import java.util.Scanner;

public class DemoTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int z = sc.nextInt();
        String str = sc.next();
        int n = str.length();
        int e;
        Graph zg = new Graph(str);
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                e = sc.nextInt();
                if(e!=0){
                    EdgeNode p = new EdgeNode(j,e);
                    zg.insertHead(i, p);//头插法
                }
            }
        }

        String c = sc.nextLine();
        c = sc.nextLine();
        int start = 0;
        for (int i = 0; i < n; i++) {
            if (str.charAt(i) == c.charAt(0)) {
                start = i;
                break;
            }
        }
        System.out.println("头插法");
        zg.printG();
        System.out.println("DFS:--------");
        zg.DFS(start);
        System.out.println();
        System.out.println("BFS:--------");
        zg.BFS(start);
    }
}
